import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";
import { EUI_Bitmap_EditTool, EUI_Bitmap_Toolbox } from "./eui_bitmap_toolbox.js";

export class EUI_Bitmap_Editor extends tt2.QUI_Panel_Scroll {
    constructor() {
        super();

        this._border.XLeft = 0;
        this._border.XRight = 0;
        this._border.YTop = 0;
        this._border.YBottom = 0;
        this.dragout = false;
        this.dragDirection = tt2.QUI_DragDriection.Both;
        this._sprite = new tt.Sprite(tt.graphic.getWhiteTexture(), null);

        this._img = new tt2.QUI_Image(this._sprite);
        this._img.localRect.setByRect(new tt.Rectangle(0, 0, 100, 100));

        let border = EUI_Style.CreateDefaultBorder();
        border.localRect.setHPosFill(2, 2);
        border.localRect.setVPosFill(2, 2);

        this.addChild(this._img)

        this.addChild(border);
    }
    private _sprite: tt.Sprite;
    //private _buffer: tt.ImageBuffer;
    private _tex: tt.ITexture;
    private _img: tt2.QUI_Image;
    tool: EUI_Bitmap_EditTool = EUI_Bitmap_EditTool.Drag;
    InitBuffer(width: number, height: number, data: Uint8Array | Uint8ClampedArray) {
        if (this._tex != null) {
            if (this._tex.getWidth() != width || this._tex.getHeight() != height) {
                this.ClearTex();
            }
        }
        if (this._tex == null) {
            this._tex = tt.graphic.CreateDynamicTexture(width, height, tt.TextureFormat.RGBA32);
        }
        this._tex.UploadTexture(0, 0, width, height, data);
        this._tex.ApplyTexture(false);
        this._sprite.tex = this._tex;
        this._sprite.pixelwidth = width;
        this._sprite.pixelheight = height;
        this._img.localRect.offsetX2 = width;
        this._img.localRect.offsetY2 = height;
        this.panelWidth = width;
        this.panelHeight = height;
    }
    OnChange: () => void;
    ClearTex() {
        if (this._tex != null) {
            this._tex.Destory();
            this._tex = null;
        }
    }
    _ondraw: boolean;
    _ondrawx: number;
    _ondrawy: number;
    drawPoint(px: number, py: number): void {
        let data = this._tex.getData();
        if (px >= 0 && px < this._tex.getWidth() && py > 0 && py < this._tex.getHeight()) {
            let seek = py * this._tex.getWidth() + px;
            data[seek * 4 + 0] = 255;
            data[seek * 4 + 1] = 1;
            data[seek * 4 + 2] = 0;

        }
    }
    drawLine(x1: number, y1: number, x2: number, y2: number) {
        let dx = Math.abs(x2 - x1);
        let dy = Math.abs(y2 - y1);

        if (dy < dx) {
            let addx = (x2 - x1) / dx;
            let addy = (y2 - y1) / dx;

            for (var i = 0; i <= dx; i++) {
                let x = x1 + addx * i;
                let y = y1 + addy * i;
                this.drawPoint(x | 0, y | 0);
            }

        }
        else {
            let addx = (x2 - x1) / dy;
            let addy = (y2 - y1) / dy;

            for (var i = 0; i <= dy; i++) {
                let x = x1 + addx * i;
                let y = y1 + addy * i;
                this.drawPoint(x | 0, y | 0);
            }
        }

    }
    OnTouch(touchid: number, press: boolean, move: boolean, x: number, y: number): boolean {
        if (this.tool == EUI_Bitmap_EditTool.Drag) {

        }
        else if (this.tool == EUI_Bitmap_EditTool.Draw) {
            if (press) {
                let rect = this.getWorldRect();
                if (this._ondraw == false) {

                    let px = (x - rect.X - this.panelOffsetX) | 0;
                    let py = (y - rect.Y - this.panelOffsetY) | 0;
                    this.drawPoint(px, py);
                    this._tex.ApplyTexture(false);
                    this._ondraw = true;
                    this._ondrawx = px;
                    this._ondrawy = py;
                }
                else {
                    let px = (x - rect.X - this.panelOffsetX) | 0;
                    let py = (y - rect.Y - this.panelOffsetY) | 0;
                    this.drawLine(this._ondrawx, this._ondrawy, px, py);
                    this._tex.ApplyTexture(false);
                    this._ondrawx = px;
                    this._ondrawy = py;
                }
            }
            else {
                this._ondraw = false;
            }
            return false;
        }
        return super.OnTouch(touchid, press, move, x, y);
    }

    OnUpdate(delta: number): void {
        {
            super.OnUpdate(delta);


        }

    }
    OnRender(_canvas: tt2.QUI_Canvas): void {
        super.OnRender(_canvas);
    }
}